﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Portal.Environment;

namespace Portal.CDSP
{
    public class FFooterBLL
    {
        public FFooterCls CreateModel(string ActiveLanguage, string HtmlId)
        {
            string Query = "select * from TableHtml where HtmlId=@HtmlId and IsPublishing=1";
            SqlConnection sqlConnection = new SqlConnection(Environments.PublicConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@HtmlId", SqlDbType.VarChar);
            sqlCommand.Parameters["@HtmlId"].Value = HtmlId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                FFooterCls HtmlCls = new FFooterCls();
                HtmlCls.HtmlId = (string)dtResult.Rows[0]["HtmlId"];
                try
                {
                    HtmlCls.Subject = (string)dtResult.Rows[0][ActiveLanguage + "Subject"];
                }
                catch { }
                try
                {
                    HtmlCls.Body = (string)dtResult.Rows[0][ActiveLanguage + "Body"];
                }
                catch { }
                try
                {
                    HtmlCls.EntryDate = (DateTime)dtResult.Rows[0]["EntryDate"];
                }
                catch { }
                try
                {
                    HtmlCls.Owner = (string)dtResult.Rows[0]["Owner"];
                }
                catch { }
                return HtmlCls;
            }
        }

        public FFooterCls CreateModelByAlias(string ActiveLanguage, string Alias)
        {
            string Query = "select * from TableHtml where Alias=@Alias and IsPublishing=1";
            SqlConnection sqlConnection = new SqlConnection(Environments.PublicConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@Alias", SqlDbType.VarChar);
            sqlCommand.Parameters["@Alias"].Value = Alias;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                string HtmlId = (string)dtResult.Rows[0]["HtmlId"];
                return CreateModel(ActiveLanguage, HtmlId);
            }
        }

        public GroupNewsItemCls CreateModelItem(string GroupNewsItemId)
        {
            string Query = "select * from TableGroupNewsItem where GroupNewsItemId=@GroupNewsItemId";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsItemCls();

                GroupNewsItemCls.GroupNewsItemId = (string)dtResult.Rows[0]["GroupNewsItemId"];
                GroupNewsItemCls.GroupNewsId = (string)dtResult.Rows[0]["fk_GroupNews_Id"];
                GroupNewsItemCls.JumpUrl = (string)dtResult.Rows[0]["JumpUrl"];
                GroupNewsItemCls.GroupNewsItemName = "";
                try
                {
                    GroupNewsItemCls.GroupNewsItemName = (string)dtResult.Rows[0][Environment.Environments.ActiveLanguage + "GroupNewsItemName"];
                }
                catch { }
                GroupNewsItemCls.IndexOrder = (int)dtResult.Rows[0]["IndexOrder"];
                try
                {
                    GroupNewsItemCls.ParentGroupNewsItemId = (string)dtResult.Rows[0]["fk_ParentGroupNewsItem_Id"];
                }
                catch { }

                try
                {
                    GroupNewsItemCls.Htmlcontent = (string)dtResult.Rows[0]["Htmlcontent"];
                }
                catch { }
                try
                {
                    GroupNewsItemCls.Images = (string)dtResult.Rows[0]["GroupNewsItemImage"];
                }
                catch { }
                try
                {
                    GroupNewsItemCls.LargeImages = (string)dtResult.Rows[0]["GroupNewsItemLargeImage"];
                }
                catch { }
                try
                {
                    GroupNewsItemCls.Description = (string)dtResult.Rows[0][Environment.Environments.ActiveLanguage + "gDescription"];
                }
                catch { }
                try
                {
                    GroupNewsItemCls.GroupNewsItemShortContent = (string)dtResult.Rows[0]["GroupNewsItemShortContent"];
                }
                catch { }
                return GroupNewsItemCls;
            }
        }

        public DataTable Reading()
        {
            string Query = "select * from TableHtml order by EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environments.PublicConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public DataTable ReadingForService(string belongSite)
        {
            string Query = "select Id=HtmlId,Subject=[" + Environments.ActiveLanguage + "Subject] from TableHtml where belongSite='" + belongSite + "' order by EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environments.PublicConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }



        public DataTable ReadingWorkflowStatus()
        {
            string Query = "select distinct WorkflowStatus from TableHtml";
            SqlConnection sqlConnection = new SqlConnection(Environments.PublicConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }
        public string RewriteCatUrl(string catUrl)
        {
            //muc đích: rewrite nc to nc1
            string reVal = catUrl;
            try
            {
                if (catUrl.Contains("-nc-"))
                {
                    reVal = catUrl.Replace("-nc-", "-nc1-");
                }
            }
            catch { }

            return reVal;
        }
        public string RewriteDetailUrl(string detailUrl)
        {
            //muc đích: rewrite -nd- to -nd2-
            string reVal = detailUrl;
            try
            {
                if (detailUrl.Contains("-nd-"))
                {
                    reVal = detailUrl.Replace("-nd-", "-nd2-");
                }
            }
            catch { }

            return reVal;
        }
        public string RewriteStaticUrl(string StaticUrl)
        {
            //muc đích: rewrite -nd- to -nd2-
            string reVal = StaticUrl;
            try
            {
                if (StaticUrl.Contains("-sthtml-"))
                {
                    reVal = StaticUrl.Replace("-sthtml-", "-sthtml2-");
                }
            }
            catch { }

            return reVal;
        }
    }
}
